//
//  SEGAdvertisementViewController.m
//  SEGUhomeStandard
//
//  Created by 刘山 on 2018/5/4.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGAdvertisementViewController.h"
#import "SEGAdvertisementView.h"
#import "SEGpassView.h"
@interface SEGAdvertisementViewController ()<SEGpassViewDelegate,protocolAdvertisementViewDelegate>
@end

@implementation SEGAdvertisementViewController{
    SEGpassView* passView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.positionType == SEGAdvlistType_SYJ) {
        [self segInitPopWindowAdView];
    }else{
        [self segInitAdView];
    }
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

-(void)segInitPopWindowAdView{
    SEGAdvertisementViewModel *data = [SEGAdvertisementViewModel new];
    data.widgetHeight = 300 * kScaleHeight;
    data.widgetWidth = 270 * kScaleWidth;
    
    data.adSource = @[self.adData];
    
    SEGAdvertisementViewLayout *layout = [[SEGAdvertisementViewLayout alloc]initWithData:data type:SEGEnumAdvertismentType_NO];
    
    SEGAdvertisementView *varView = [[SEGAdvertisementView alloc]initWithFrame:CGRectZero];
    varView.delegate = self;
    varView.left = (LYScreenWidth() - data.widgetWidth) * 0.5;
    varView.top = (LYScreenHeight() - data.widgetHeight) * 0.5;
    [varView setWithLayout:layout];
    
    [self.view addSubview:varView];
    
    varView.layer.masksToBounds = YES;
    varView.layer.cornerRadius = 4;
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:closeBtn];
    [closeBtn setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_dialog_close_gray")] forState:UIControlStateNormal];
    [closeBtn sizeToFit];
    [closeBtn addTarget:self action:@selector(seg_Click:) forControlEvents:UIControlEventTouchUpInside];
    closeBtn.frame = CGRectMake(CGRectGetMaxX(varView.frame) - closeBtn.imageView.image.size.width - 10,
                                CGRectGetMinY(varView.frame) + 10,
                                closeBtn.imageView.image.size.width,
                                closeBtn.imageView.image.size.height);
}

-(void)seg_Click:(UIButton*)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)segInitAdView{
    
    SEGAdvertisementViewModel *data = [SEGAdvertisementViewModel new];
    data.widgetHeight = self.view.height;
    data.widgetWidth = self.view.width;
   
    data.adSource = @[self.adData];
    
    SEGAdvertisementViewLayout *layout = [[SEGAdvertisementViewLayout alloc]initWithData:data type:SEGEnumAdvertismentType_NO];
    
    SEGAdvertisementView *varView = [[SEGAdvertisementView alloc]initWithFrame:CGRectZero];
    varView.delegate = self;
    varView.left = 0;
    [varView setWithLayout:layout];
    
    [self.view addSubview:varView];
    
    
    passView = [[SEGpassView alloc] initWithFrame:CGRectMake(LYScreenWidth() - (50 + 18), 29, 50, 50)];
    passView.delegate = self;
    passView.passTime = self.adData.runingSecond.integerValue;
    [self.view addSubview:passView];
}

#pragma mark - SEGpassViewDelegate
- (void)click_SEGpassView{
    [self dismissViewControllerAnimated:YES completion:^{
        UIViewController *VC = [[SEGUhomeMediator sharedInstance]seg_getCurrentController:nil];
        NSDictionary *params = @{@"position":@(SEGAdvlistType_SYJ),@"vc":VC};
        [[SEGUhomeMediator sharedInstance]seg_showLaunchAndPopWindowAdvertisement:params];
    }];
}

#pragma mark - protocolAdvertisementViewDelegate
-(void)segProtocolAdvertismentClick:(SEGAdvertismentModel*)sender{
    [self dismissViewControllerAnimated:YES completion:^{
        [[SEGUhomeMediator sharedInstance] seg_pushToViewControllerWithType:[sender yy_modelToJSONObject]];
        if (self.positionType == SEGAdvlistType_START) {
            self->passView.delegate = nil;
            UIViewController *VC = [[SEGUhomeMediator sharedInstance]seg_getCurrentController:nil];
            NSDictionary *params = @{@"position":@(SEGAdvlistType_SYJ),@"vc":VC};
            [[SEGUhomeMediator sharedInstance]seg_showLaunchAndPopWindowAdvertisement:params];

        }
    }];
}

-(SEGAdvertismentModel *)adData{
    if (!_adData) {
        _adData = [SEGAdvertismentModel new];
    }
    return _adData;
}
@end
